package com.lzp.order.controller;


import com.lzp.api.dto.OrderDTO;
import com.lzp.common.result.PageResult;
import com.lzp.common.result.Result;
import com.lzp.order.pojo.vo.OrderVO;
import com.lzp.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
@RequiredArgsConstructor
public class OrderController {

    private final IOrderService orderService;

    @GetMapping("/{id}")
    @ApiOperation("根据id获取订单")
    public Result<OrderVO> getOrder(@PathVariable("id") Long id){
        return Result.success(orderService.getOrderById(id));
    }

    @GetMapping("/list")
    @ApiOperation("获取全部订单")
    public Result<PageResult> list(){
        return Result.success(orderService.queryAllOrders());
    }

    @PostMapping("/save")
    @ApiOperation("新增订单")
    public Result<String> save(@Validated @RequestBody OrderDTO order){
        return orderService.addOrder(order) ? Result.success("添加成功") : Result.error("添加失败");
    }

    @PutMapping("/update")
    @ApiOperation("更新订单")
    public Result<String> update(@RequestBody OrderDTO order){
        return orderService.updateOrder(order) ? Result.success("更新成功") : Result.error("更新失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除订单")
    public Result<String> delete(@PathVariable("id") Long id){
        return orderService.deleteOrder(id) ? Result.success("删除成功") : Result.error("删除失败");
    }


}
